package shanghai;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.util.HashMap;
import java.util.Map;

public class HttpSpiderShanghaiJinan {
    public static void main(String[] args) {
        Map<String, Object> paramData =new HashMap<String, Object>();
        paramData.put("pageSize", "10");
        paramData.put("itemType", "");
        paramData.put("orgCode", "");
        paramData.put("stRegion", "SH00JA");
        paramData.put("sort", "");
        paramData.put("stKeyword","");


        int pageNum = 269;

        for (int i = 1; i < pageNum+1; i++) {
            paramData.put("pageNumber", i);
            String httpRequest = HttpRequest.post("http://zwdt.sh.gov.cn/govPortals/item/getItemPowerByQuery.do")
                    .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0")
                    .form(paramData)
                    .execute()
                    .body();


            JSONObject jsonObject = JSONUtil.parseObj(httpRequest);
            JSONArray item = jsonObject.getJSONArray("item");
            //JSONObject pageList = data.getJSONObject("list");
            System.err.println(StrUtil.format("-------------第{}页返回结果------------------", i));
            System.out.println(item);
            FileUtil.writeUtf8String(item.toStringPretty(), "C:\\csvshanghaijinan300\\" + i + ".json");
            ThreadUtil.sleep(1000);
        }






       /* JSONArray records = pageList.getJSONArray("records");
        CsvWriter writer = new CsvWriter("E:\\hubei\\2.csv");
        for (int i = 0; i < records.size(); i++) {
            String [] strings=new String[2];
            JSONObject obj= records.getJSONObject(i);
            String taskcode = obj.getStr("TASKCODE");
            String mulu = obj.getStr("MULU");
            strings [0]=taskcode;
            strings [1]=mulu;
            writer.write(strings);
        }
        writer.close();
        System.out.println(records.toStringPretty());*/

    }
}
